import { Result } from "~/models/HomePage/index";
import { updatePermision } from "~/utils/permision"

function login(instance, username, password) {
    const _this = instance
    return new Promise((resolve, reject) => {
        _this.$axios({
            url: "/api/oauth/login",
            method: "post",
            data: {
                username, password
            },
        })
            .then(async res => {
                const resultToken = res.data.token;
                let result: Result = {
                    status: 0, //0是成功
                    data: res
                };
                if (res && resultToken) {
                    // 获取权限数据、并保存到本地
                    // updatePermision(_this, '')
                    // this._this.$store.commit("setToken", resultToken);
                    _this.$store.commit("authentication/setToken", resultToken)
                    _this.$cookies.set("token", resultToken);
                    resolve(result);
                } else {
                    result.status = 2
                    result.data = res.data;
                    resolve((result));
                }
            }).catch(err => {
                reject(err);
            })
    })
}

export { login };
